package cn.rui.controller;

import cn.rui.constant.MessageConstant;
import cn.rui.entity.PageResult;
import cn.rui.entity.QueryPageBean;
import cn.rui.entity.Result;
import cn.rui.pojo.Role;
import cn.rui.service.RoleService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理
 *
 * @author 徽州大都督
 * @date 2020/9/2
 */
@RestController
@RequestMapping("role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 查询全部角色信息
     *
     * @return
     */
    @GetMapping("findAllRoleName")
    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    public List<Role> roleService() {

        return roleService.findAllRoleName ();
    }

    /**
     * 分页查询
     */
    @PostMapping("findPage")
    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return roleService.findPage (queryPageBean);
    }

    /**
     * 根据id查询
     */
    @GetMapping("findById/{id}")
    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    public Result findById(@PathVariable("id") Integer id) {

        try {
            Role role = roleService.findById (id);
            return new Result (true, MessageConstant.GET_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace ();
            return new Result (true, MessageConstant.GET_ROLE_FAIL);

        }
    }

    /**
     * 根据角色id查询关联的执行权限id
     */
    @GetMapping("findPermissionsByRoleId/{id}")
    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    public Result findPermissionsByRoleId(@PathVariable("id") Integer id) {

        try {
            List<Integer> permissionIds = roleService.findPermissionsByRoleId (id);
            return new Result (true, "查询权限id集合成功", permissionIds);
        } catch (Exception e) {
            e.printStackTrace ();
            return new Result (false, "查询权限id集合失败");
        }
    }

    @GetMapping("findUrlsByRoleId/{id}")
    @PreAuthorize("hasAnyAuthority('ROLE_QUERY')")
    public Result findUrlsByRoleId(@PathVariable("id") Integer id) {

        try {
            List<Integer> menuIds = roleService.findUrlsByRoleId (id);
            return new Result (true, "查询关联菜单成功", menuIds);
        } catch (Exception e) {
            e.printStackTrace ();
            return new Result (false, "查询关联菜单失败");
        }
    }

    /**
     * 添加角色
     */
    @PostMapping("add/{menuIds}/{permissionIds}")
    @PreAuthorize("hasAnyAuthority('ROLE_ADD')")
    public Result add(@PathVariable("menuIds") List<Integer> menuIds, @PathVariable("permissionIds") List<Integer> permissionIds, @RequestBody Role role) {

        try {
            roleService.add (menuIds, permissionIds, role);

            return new Result (true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace ();

            return new Result (true, MessageConstant.ADD_ROLE_FAIL);
        }


    }

    /**
     * 编辑角色
     */
    @PostMapping("edit/{menuIds}/{permissionIds}")
    @PreAuthorize("hasAnyAuthority('ROLE_EDIT')")
    public Result edit(@PathVariable("menuIds") List<Integer> menuIds, @PathVariable("permissionIds") List<Integer> permissionIds, @RequestBody Role role) {
        try {
            roleService.edit (menuIds, permissionIds, role);

            return new Result (true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace ();

            return new Result (true, MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    /**
     * 根据id删除角色
     */
    @GetMapping("delete/{id}")
    @PreAuthorize("hasAnyAuthority('ROLE_DELETE')")
    public Result delete(@PathVariable("id")Integer id){

        try {
            roleService.delete(id);
            return new Result (true,MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace ();
            return new Result (false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }
}
